import productList from '../../data/productList.js'
import Toast from '/@vant/weapp/toast/toast';
const productUtil = require('../../utils/productUtil.js');
const userUtil = require('../../utils/user.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        cartProductList: [],
        priceSum: 0,
        showEditAddress: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getTabBar().init();
        this.updateCartProductList()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    updateCartProductList() {
        let cartProductList = productUtil.getCacheCartProductIdList()
        console.log(cartProductList)

        this.getTabBar().setCartNumber(cartProductList.size)
        let tempPriceSum = 0
        let tempProductList = productList.filter(x => {
            return cartProductList.has(String(x.id))
        }).map(x => {
            x.number = productUtil.getCartProductNum(x.id)
            x.itemCount = x.number * x.price
            tempPriceSum = x.itemCount + tempPriceSum
            // x.productName = x.productName.replace('（1斤）', '')
            return x
        })


        this.setData({
            cartProductList: tempProductList,
            priceSum: tempPriceSum
        })
    },
    productNumberChanger(event) {
        let productNumber = event.detail
        let productid = event.currentTarget.dataset.productid
        console.log(productid, productNumber)
        productUtil.setCartProductNum(productid, productNumber)
        if (productNumber == 0) {
            productUtil.removeFromCartProductList(productid)

        }
        this.updateCartProductList()
    },

    copyCart() {

        let item_price = ''
        let purchaseProductList = this.data.cartProductList.map(x => {
            
            return `${x.productName} * ${x.number}`
        })
        let pr = purchaseProductList.join('\n')
        let freight_sum = 0
        let good_weight = 0
 
        this.data.cartProductList.forEach(item => {
     
            item_price = item_price + item.price *  item.number + '+'
            good_weight = item.number * item.weight + good_weight
        })

        if (good_weight >= 5000) {
            freight_sum = Math.ceil(good_weight / 500) * 2.5 / 2
        } else {
            freight_sum = Math.ceil(good_weight / 500) * 2.5
        }
        const totalPrice = this.data.cartProductList.reduce((sum, obj) => sum + obj.itemCount, 0) 
        


        let c = `${pr}\n[商品总价（元）]  ${item_price.slice(0,-1)} = ${totalPrice}\n[总重量:（公斤）]  ${good_weight/1000}\n[运费（元）]  ${freight_sum}\n[总计（元）:商品总价+运费]  ${totalPrice + freight_sum}\n`


        wx.setClipboardData({
            data: c,
            success: function () {
                Toast('点击右下角 联系客服 粘贴 下单')
            }
        })

    }
})